package zack.gateway.center.infrastructure.repository;


import org.springframework.stereotype.Component;

import zack.gateway.center.domain.manage.model.vo.GatewayServerDetailVO;
import zack.gateway.center.domain.manage.model.vo.GatewayServerVO;
import zack.gateway.center.domain.manage.repository.IConfigManageRepository;
import zack.gateway.center.domain.manage.model.vo.ApplicationInterfaceMethodVO;
import zack.gateway.center.domain.manage.model.vo.ApplicationInterfaceVO;
import zack.gateway.center.domain.manage.model.vo.ApplicationSystemVO;
import zack.gateway.center.domain.manage.repository.InterfaceKey;
import zack.gateway.center.infrastructure.dao.*;
import zack.gateway.center.infrastructure.po.*;

import javax.annotation.Resource;
import java.sql.Array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author   zack
 * @description 网关配置仓储服务
   */
@Component
public class ConfigManageRepository implements IConfigManageRepository {

    @Resource
    private IGatewayServerDao gatewayServerDao;
    @Resource
    private IGatewayServerDetailDao gatewayServerDetailDao;
    @Resource
    IApplicationSystemDao applicationSystemDao;
    @Resource
    IApplicationInterfaceDao applicationInterfaceDao;
    @Resource
    IApplicationInterfaceMethodDao applicationInterfaceMethodDao;
    @Resource
    IGatewayDistributionDao gatewayDistributionDao;


    @Override
    public List<GatewayServerVO> queryGatewayServerList() {
        List<GatewayServer> gatewayServers = gatewayServerDao.queryGatewayServerList();
        List<GatewayServerVO> gatewayServerVOList = new ArrayList<>(gatewayServers.size());
        for (GatewayServer gatewayServer : gatewayServers) {
            // 可以按照 IDEA 插件 vo2dto 方便转换
            GatewayServerVO gatewayServerVO = new GatewayServerVO();
            gatewayServerVO.setGroupId(gatewayServer.getGroupId());
            gatewayServerVO.setGroupName(gatewayServer.getGroupName());
            gatewayServerVOList.add(gatewayServerVO);
        }
        return gatewayServerVOList;
    }

    @Override
    public boolean registerGatewayServerNode(String groupId, String gatewayId, String gatewayName, String gatewayAddress, Integer status) {
        GatewayServerDetail gatewayServerDetail = new GatewayServerDetail();
        gatewayServerDetail.setGroupId(groupId);
        gatewayServerDetail.setGatewayId(gatewayId);
        gatewayServerDetail.setGatewayName(gatewayName);
        gatewayServerDetail.setGatewayAddress(gatewayAddress);
        gatewayServerDetail.setStatus(status);
        gatewayServerDetailDao.insert(gatewayServerDetail);
        return true;
    }

    @Override
    public GatewayServerDetailVO queryGatewayServerDetail(String gatewayId, String gatewayAddress) {
        GatewayServerDetail req = new GatewayServerDetail();
        req.setGatewayId(gatewayId);
        req.setGatewayAddress(gatewayAddress);
        GatewayServerDetail gatewayServerDetail = gatewayServerDetailDao.queryGatewayServerDetail(req);
        if (null == gatewayServerDetail) return null;
        // 可以按照 IDEA 插件 vo2dto 方便转换
        GatewayServerDetailVO gatewayServerDetailVO = new GatewayServerDetailVO();
        gatewayServerDetailVO.setGatewayId(gatewayServerDetail.getGatewayId());
        gatewayServerDetailVO.setGatewayName(gatewayServerDetail.getGatewayName());
        gatewayServerDetailVO.setGatewayAddress(gatewayServerDetail.getGatewayAddress());
        gatewayServerDetailVO.setStatus(gatewayServerDetail.getStatus());
        return gatewayServerDetailVO;
    }

    @Override
    public boolean updateGatewayStatus(String gatewayId, String gatewayAddress, Integer available) {
        GatewayServerDetail gatewayServerDetail = new GatewayServerDetail();
        gatewayServerDetail.setGatewayId(gatewayId);
        gatewayServerDetail.setGatewayAddress(gatewayAddress);
        gatewayServerDetail.setStatus(available);
        return gatewayServerDetailDao.updateGatewayStatus(gatewayServerDetail);
    }

    /**
     * 根据网关id来查询与该网关关联的rpc应用id
     * @param gatewayId
     * @return
     */
    @Override
    public List<String> queryGatewayDistributionSystemIdList(String gatewayId) {
        return gatewayDistributionDao.queryGatewayDistributionSystemIdList(gatewayId);

    }

    /**
     * 根据systemId来查询rpc应用
     * @param systemIdList
     * @return
     */
    @Override
    public List<ApplicationSystemVO> queryApplicationSystemList(List<String> systemIdList) {
        List<ApplicationSystemVO> applicationSystemVOS = new ArrayList<>();
        List<ApplicationSystem> applicationSystems = applicationSystemDao.queryApplicationSystemList(systemIdList);
        for(ApplicationSystem applicationSystem : applicationSystems ) {
            ApplicationSystemVO applicationSystemVO = new ApplicationSystemVO();
            applicationSystemVO.setSystemId(applicationSystem.getSystemId());
            applicationSystemVO.setSystemName(applicationSystem.getSystemName());
            applicationSystemVO.setSystemType(applicationSystem.getSystemType());
            applicationSystemVO.setSystemRegistry(applicationSystem.getSystemRegistry());
            applicationSystemVOS.add(applicationSystemVO);
        }
        return applicationSystemVOS;
    }

    @Override
    public List<ApplicationInterfaceVO> queryApplicationInterfaceList(String systemId) {
        return Collections.emptyList();
    }

    @Override
    public List<ApplicationInterfaceVO> queryApplicationInterfaceList(List<String> interfaceIdList) {
        List<ApplicationInterfaceVO> applicationInterfaceVOS = new ArrayList<>();
        List<ApplicationInterface> applicationInterfaces = applicationInterfaceDao.queryApplicationInterfaceList(interfaceIdList);
        for(ApplicationInterface applicationInterface : applicationInterfaces ) {
            ApplicationInterfaceVO applicationInterfaceVO = new ApplicationInterfaceVO();
            applicationInterfaceVO.setSystemId(applicationInterface.getSystemId());
            applicationInterfaceVO.setInterfaceId(applicationInterface.getInterfaceId());
            applicationInterfaceVO.setInterfaceName(applicationInterface.getInterfaceName());
            applicationInterfaceVO.setInterfaceVersion(applicationInterface.getInterfaceVersion());
            applicationInterfaceVOS.add(applicationInterfaceVO);
        }
        return applicationInterfaceVOS;
    }

    @Override
    public List<ApplicationInterfaceMethodVO> queryApplicationInterfaceMethodList(String systemId, String interfaceId) {
        return Collections.emptyList();
    }

    @Override
    public List<ApplicationInterfaceMethodVO> queryApplicationInterfaceMethodListByKeys(List<InterfaceKey> keys) {
        List<ApplicationInterfaceMethodVO> interfaceMethodVOS = new ArrayList<>();
        List<ApplicationInterfaceMethod> interfaceMethods = applicationInterfaceMethodDao.queryApplicationInterfaceMethodListByKeys(keys);
        for(ApplicationInterfaceMethod applicationInterfaceMethod : interfaceMethods ) {
            ApplicationInterfaceMethodVO applicationInterfaceMethodVO  = new ApplicationInterfaceMethodVO();
            applicationInterfaceMethodVO.setSystemId(applicationInterfaceMethod.getSystemId());
            applicationInterfaceMethodVO.setInterfaceId(applicationInterfaceMethod.getInterfaceId());
            applicationInterfaceMethodVO.setMethodId(applicationInterfaceMethod.getMethodId());
            applicationInterfaceMethodVO.setMethodName(applicationInterfaceMethod.getMethodName());
            applicationInterfaceMethodVO.setParameterType(applicationInterfaceMethod.getParameterType());
            applicationInterfaceMethodVO.setUri(applicationInterfaceMethod.getUri());
            applicationInterfaceMethodVO.setHttpCommandType(applicationInterfaceMethod.getHttpCommandType());
            applicationInterfaceMethodVO.setAuth(applicationInterfaceMethod.getAuth());
            interfaceMethodVOS.add(applicationInterfaceMethodVO);
        }
        return interfaceMethodVOS;
    }

    @Override
    public String queryGatewayDistributionBySystemId(String systemId) {
       return gatewayDistributionDao.queryGatewayDistributionBySystemId(systemId);
    }

    @Override
    public List<GatewayServerDetailVO> queryGatewayServerDetailList() {
       List<GatewayServerDetail> gatewayServerVOList = gatewayServerDetailDao.queryGatewayServerDetailList();
       List<GatewayServerDetailVO> gatewayServerDetailVOS = new ArrayList<>();
       for(GatewayServerDetail gatewayServerDetail : gatewayServerVOList ) {
           GatewayServerDetailVO gatewayServerDetailVO = new GatewayServerDetailVO();
           gatewayServerDetailVO.setGatewayId(gatewayServerDetail.getGatewayId());
           gatewayServerDetailVO.setGatewayName(gatewayServerDetail.getGatewayName());
           gatewayServerDetailVO.setGatewayAddress(gatewayServerDetail.getGatewayAddress());
           gatewayServerDetailVO.setStatus(gatewayServerDetail.getStatus());
           gatewayServerDetailVO.setId(gatewayServerDetail.getId());
           gatewayServerDetailVO.setGroupId(gatewayServerDetail.getGroupId());
           gatewayServerDetailVO.setCreateTime(gatewayServerDetail.getCreateTime());
           gatewayServerDetailVO.setUpdateTime(gatewayServerDetail.getUpdateTime());
           gatewayServerDetailVOS.add(gatewayServerDetailVO);
       }
       return gatewayServerDetailVOS;
    }

    @Override
    public ApplicationSystemVO queryApplicationSystemBySystemId(String systemId) {
       ApplicationSystem system = applicationSystemDao.queryApplicationSystemBySystemId(systemId);
       if(system == null) {
           return null;
       }
       ApplicationSystemVO applicationSystemVO = new ApplicationSystemVO();
       applicationSystemVO.setSystemId(system.getSystemId());
       applicationSystemVO.setSystemName(system.getSystemName());
       applicationSystemVO.setSystemType(system.getSystemType());
       applicationSystemVO.setSystemRegistry(system.getSystemRegistry());

       return applicationSystemVO;
    }

    @Override
    public void distributionGatewayServerNode(String groupId, String gatewayId, String systemId, String systemName) {
        GatewayDistribution gatewayDistribution = new GatewayDistribution();
        gatewayDistribution.setGroupId(groupId);
        gatewayDistribution.setGatewayId(gatewayId);
        gatewayDistribution.setSystemId(systemId);
        gatewayDistribution.setSystemName(systemName);
        gatewayDistributionDao.insert(gatewayDistribution);

    }
}
